package com.quyeying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.quyeying.entity.TblFollowMerchantEntity;
import com.quyeying.entity.TblMessageEntity;
import com.quyeying.mapper.TblFollowMerchantDao;
import com.quyeying.service.TblFollowMerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quyeying.service.TblMessageService;
import com.quyeying.utils.TokenUtil;
import org.springframework.stereotype.Service;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* <p>
 * 关注商户表 服务实现类
 * </p>
*
* @author jll
* @since 2022-12-14
*/
@Service
public class TblFollowMerchantServiceImpl extends ServiceImpl<TblFollowMerchantDao, TblFollowMerchantEntity> implements TblFollowMerchantService {

    @Resource
    private TblMessageService tblMessageService;

    @Override
    public R findListByPage(Integer page, Integer pageCount){
        IPage<TblFollowMerchantEntity> wherePage = new Page<>(page, pageCount);
        TblFollowMerchantEntity where = new TblFollowMerchantEntity();
        IPage<TblFollowMerchantEntity> iPage = baseMapper.selectPage(wherePage, Wrappers.query(where));
        return R.success(ReturnPageData.fillingData(iPage));
    }

    @Override
    public R add(TblFollowMerchantEntity tblFollowMerchantEntity){
        baseMapper.insert(tblFollowMerchantEntity);
        return R.success();
    }

    @Override
    public R delete(Long id){
        baseMapper.deleteById(id);
        return R.success();
    }

    @Override
    public R updateData(TblFollowMerchantEntity tblFollowMerchantEntity){
        baseMapper.updateById(tblFollowMerchantEntity);
        return R.success();
    }

    @Override
    public R findById(Long id){
        TblFollowMerchantEntity tblFollowMerchantEntity = baseMapper.selectById(id);
        return R.success(tblFollowMerchantEntity);
    }

    /**
     * 用户是否关注商户接口
     *
     * @param userId
     * @param merchantId
     */
    @Override
    public boolean isFollow(Long userId, Long merchantId) {
        QueryWrapper<TblFollowMerchantEntity> followMerchantEntityQueryWrapper = new QueryWrapper<>();
        followMerchantEntityQueryWrapper.eq("user_id", userId).eq("merchant_id", merchantId);
        TblFollowMerchantEntity followMerchant = getOne(followMerchantEntityQueryWrapper);
        return ObjectUtils.isNotEmpty(followMerchant);
    }

    @Override
    public R findListByIdPage(Integer current, Integer size, HttpServletRequest request) {
        long userId = TokenUtil.getUserId(request);
        IPage<Map<String,Object>> tblMessageEntities =baseMapper.findListByIdPage(new Page<>(current,size),userId);
        QueryWrapper<TblMessageEntity> queryWrapper = new QueryWrapper<>();
        tblMessageEntities.getRecords().stream().forEach(item ->{
            //消息
            queryWrapper.clear();
            queryWrapper.eq("merchant_id", item.get("id")).eq("user_id", userId).eq("is_read", 0);
            TblMessageEntity messageEntity = tblMessageService.getOne(queryWrapper);
            item.put("messageEntity",messageEntity);
        });
        return R.success(tblMessageEntities);
    }
}
